package com.castudio.Service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castudio.Common.RespPageEntity;
import com.castudio.Entity.Activity;
import com.castudio.Entity.User;
import com.castudio.Mapper.ActivityMapper;
import com.castudio.Service.ActivityService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小陈同学
 * @since 2023-01-19
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public RespPageEntity getAllActivities(Integer page, Integer size, String title) {
        RespPageEntity pageUtils = new RespPageEntity();
        // 默认从0开始
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        // 获取当前也用户信息
        List<Activity> activities = activityMapper.getAllActivities(page, size, title);
        pageUtils.setData(activities);
        // 获取当前用户总量
        Long total = activityMapper.getTotal(title);
        pageUtils.setTotal(total);

        return pageUtils;
    }

    @Override
    public void save(String fileUrl, String title, String profile, String author, String categories,
                     String content) {
        activityMapper.insert(fileUrl, title, profile, author, categories, content);
    }

    @Override
    public void saveImage(String image) {
        activityMapper.insertImg(image);
    }

    @Override
    public IPage<Activity> getPage(Integer currentPage, Integer PageSize, Activity activity) {
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<Activity>();
        queryWrapper.like(Strings.isNotEmpty(activity.getTitle()), Activity ::getTitle, activity.getTitle());
        IPage page = new Page(currentPage, PageSize);
        activityMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public IPage<Activity> getPage(Integer currentPage, Integer pageSize) {
        IPage page = new Page(currentPage, pageSize);
        activityMapper.selectPage(page, null);
        return page;
    }
}


